/**
 * 
 */
package com.Adapters.Items;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.Animations.AnimUtil;
import com.Animations.AnimUtil.AnimationCompleted;
import com.Animations.AnimationLists;
import com.Data.Albums;
import com.Data.Albums.onDeleteCompleted;
import com.Dialogs.DialogAlbum;
import com.Dialogs.DialogOneText;
import com.Dialogs.DialogOneText.OnClickOkListener;
import com.Init.ImageDownloaderInit;
import com.Managers.FragmentManagerClass;
import com.SoapClasses.SoapUtil;
import com.TextFormat.UtilFonts;
import com.activities.ActivityAlbumDetail;
import com.activities.AlbumFunctions;
import com.activities.DefaultFunctions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.shareup.R;
import com.utils.ShowToastClass;


/**
 * @author Chris
 * 
 */
public class ItemAlbum implements ItemPost {

    private ImageLoader imgLoader;

    public Albums getAlbum() {

        return this.album;
    }

    public void setAlbum(Albums album) {

        this.album = album;
    }

    public static boolean wasDestroyed;

    private Albums album;

    private int pos;

    private Context context;

    private ViewHolder holder;

    private int imageWidth;

    private LayoutInflater inflater;

    private DefaultFunctions main;

    private AlbumFunctions funcs;

    private OnClickListener onAlbumClick = new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (album.getPin() != 0) {

                final String error = context.getResources().getString(R.string.error_pin_wrong);
                writePin(new OnClickOkListener() {

                    @Override
                    public void OnClick(String text) {

                        int pin = Integer.parseInt(text);
                        if (pin == album.getPin())
                            continueWithClick();
                        else
                            ShowToastClass.message(error, context);

                    }
                });

            }
            else
                continueWithClick();
        };
    };

    private void continueWithClick() {

        boolean expectingResult = false;
        if (FragmentManagerClass.aAlbum != null)
            expectingResult = FragmentManagerClass.aAlbum.isExpectingResult;
        // IF WE WANT THE ID OF THE ALBUM
        if (expectingResult) {
            // FragmentManagerClass.aAlbum.putResult(Activity.RESULT_OK,
            // album);
            funcs.setAlbum(album);
        }
        else {

            SoapUtil.ALBUM_ID = album.getID();
            SoapUtil.MENU_VIEW = SoapUtil.MODE_ALBUM;
            Intent intent = new Intent(context, ActivityAlbumDetail.class);
            ActivityAlbumDetail.album = album;
            context.startActivity(intent);
            AnimUtil.getActivityTransition(FragmentManagerClass.aAlbum, true);
        }

    }

    private OnLongClickListener onAlbumLongClick = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            showDialogAlbum();

            return false;
        }

    };

    private void showDialogAlbum() {

        if (album.getPin() != 0) {

            final String error = context.getResources().getString(R.string.error_pin_wrong);
            writePin(new OnClickOkListener() {

                @Override
                public void OnClick(String text) {

                    int pin = Integer.parseInt(text);
                    if (pin == album.getPin())
                        showDialogAlbumOptions();
                    else
                        ShowToastClass.message(error, context);

                }
            });

        }
        else
            showDialogAlbumOptions();

    }

    private void showDialogAlbumOptions() {

        DialogAlbum da = new DialogAlbum(context, this);
        da.show();
    }

    private void writePin(OnClickOkListener callback) {

        DialogOneText ded = new DialogOneText(context, callback);
        ded.setType(InputType.TYPE_CLASS_NUMBER);
        ded.setHint(context.getResources().getString(R.string.crop_enter_pin));
        ded.setMinLength(4);
        ded.setMaxLength(4);
        ded.show();
    }

    public void showInfo() {

        funcs.showInfo(album);
    }

    class ViewHolder {

        public LinearLayout container;

        public TextView name;

        public RelativeLayout nameContainer;

        public ProgressBar progress;

        public ImageView thumb;
    }

    public ItemAlbum(int columnWidth, LayoutInflater inflater,
                Albums album, DefaultFunctions main,
                AlbumFunctions funcs, ImageLoader loader) {

        super();
        this.imageWidth = columnWidth;
        this.album = album;
        this.inflater = inflater;
        this.context = main.getParentActivity();
        this.main = main;
        this.funcs = funcs;
        this.imgLoader = loader;

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateIn()
     */
    @Override
    public void AnimateIn() {

    }

    private void Animate(Bitmap image) {

        AnimUtil.AnimateFlyImage(holder.thumb, pos, image);
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateOut(com.Animations.AnimUtil.
     * AnimationCompleted)
     */
    @Override
    public void AnimateOut(AnimationCompleted callback) {

        AnimUtil.PostOut(context, holder.container, callback);

    }

    public void changeCover() {

    }

    public void changePin() {

    }

    public void changePrivacy() {

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#clearAnimatonList()
     */
    @Override
    public void clearAnimatonList() {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#delete()
     */
    @Override
    public void delete() {

        AlertDialog.Builder dg = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_actions, null);
        TextView message = (TextView) v.findViewById(R.id.message_dialog);
        UtilFonts.setTextRegularFont(message);
        Button accept = (Button) v.findViewById(R.id.dialog_action_a_button);
        UtilFonts.setTextRegularFont(accept);
        Button cancel = (Button) v.findViewById(R.id.dialog_action_c_button);
        UtilFonts.setTextRegularFont(cancel);
        //
        message.setText(R.string.dialog_question_delete_album_message);
        //
        dg.setView(v);
        //
        final AlertDialog alert = dg.create();
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                main.remove(album, new onDeleteCompleted() {

                    @Override
                    public void Completed(String result) {

                        main.remove(album);
                        holder.nameContainer.setVisibility(View.VISIBLE);
                        holder.container.setVisibility(View.VISIBLE);
                        holder.thumb.setVisibility(View.VISIBLE);

                    }
                });

                alert.dismiss();
            }
        });
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                alert.dismiss();
            }
        });
        //
        //
        //
        alert.show();

    }

    public void display(ImageView image, String url, final ProgressBar progressbar) {

        imgLoader.displayImage(url, image, ImageDownloaderInit.getOptionsForGrid(), new ImageLoadingListener() {

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                progressbar.setVisibility(View.GONE);

                if (loadedImage != null) {

                    boolean firstDisplay = !AnimationLists.displayedThumbs.contains(imageUri);
                    if (firstDisplay) {
                        Animate(loadedImage);
                        AnimationLists.displayedThumbs.add(imageUri);

                    }

                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                progressbar.setVisibility(View.GONE);

            }

            @Override
            public void onLoadingStarted(String imageUri, View view) {

                // progressbar.setVisibility(View.VISIBLE); // set the
                // spinner
                // visible
            }

        });

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getCommentsCount()
     */
    @Override
    public int getCommentsCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getLikesCount()
     */
    @Override
    public int getLikesCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPostId()
     */
    @Override
    public int getPostId() {

        return album.getID();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPublicationDate()
     */
    @Override
    public String getPublicationDate() {

        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getView(android.view.View)
     */
    @Override
    public View getView(View convertView, int position) {

        View view = null;
        pos = position;
        if (convertView == null) {

            ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.item_album, null);
            holder = new ViewHolder();
            holder.container = (LinearLayout) viewGroup.findViewById(R.id.container);
            holder.name = (TextView) viewGroup.findViewById(R.id.name);
            holder.nameContainer = (RelativeLayout) viewGroup.findViewById(R.id.name_container);
            holder.progress = (ProgressBar) viewGroup.findViewById(R.id.loading_progress);
            holder.thumb = (ImageView) viewGroup.findViewById(R.id.thumb);
            holder.thumb.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.thumb.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth, imageWidth));
            UtilFonts.setTextRegularFont(holder.name);
            viewGroup.setTag(holder);
            view = viewGroup;

        }
        else {

            holder = (ViewHolder) convertView.getTag();
            view = convertView;
            holder.thumb.setImageDrawable(null);

        }

        holder.thumb.setTag(position);

        setTexts();

        return view;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getViewType()
     */
    @Override
    public int getViewType() {

        return RowType.ALBUM.ordinal();
    }

    public void rename() {

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setCommentsCount(int)
     */
    @Override
    public void setCommentsCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setLikesCount(int)
     */
    @Override
    public void setLikesCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#updatePublicationDate(java.lang.String)
     */
    @Override
    public void updatePublicationDate(String newDate) {

        // TODO Auto-generated method stub

    }

    private void showNoCover() {

        holder.progress.setVisibility(View.GONE);
        holder.thumb.setImageDrawable(new ColorDrawable(context.getResources().getColor(R.color.profile_rec)));
    }

    private void setTexts() {

        holder.name.setText(album.getName());
        holder.container.setOnClickListener(onAlbumClick);
        holder.container.setOnLongClickListener(onAlbumLongClick);

        if (album.getPin() != 0)// IF THE ALBUM HAS A PIN
            showNoCover();
        else
            if (album.getCover().equals("0"))
                showNoCover();
            else
                display(holder.thumb, album.getCover(), holder.progress);

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPosition()
     */
    @Override
    public int getPosition() {

        // TODO Auto-generated method stub
        return 0;
    }

}
